package com.kongyz;

import com.kongyz.shape.Circle;
import com.kongyz.shape.Rectangle;
import com.kongyz.shape.Shape;
import com.kongyz.shape.Square;

import java.util.Hashtable;

/**
 * @author kongyz
 * @date 2018/6/22 21:23
 * @Description: 从IO（数据库或文件等）获取实体类，并把他们储存在一个Hashtabel中
 */
public class ShapeCache {

    private static Hashtable<String,Shape> shapeMap=new Hashtable<String, Shape>();

    /**
     * 获取实体的克隆
     * @param shapeId
     * @return
     */
    public static Shape getShape(String shapeId){
        Shape cachedShape=shapeMap.get(shapeId);
        return (Shape) cachedShape.clone();
    }

    /**
     * 对每种形状都运行数据库查询，并创建该形状
     * 添加到hashtable中
     */
    public static void loadCache(){
        Circle circle=new Circle();
        circle.setId("1");
        shapeMap.put(circle.getId(),circle);

        Square square=new Square();
        square.setId("2");
        shapeMap.put(square.getId(),square);

        Rectangle rectangle=new Rectangle();
        rectangle.setId("3");
        shapeMap.put(rectangle.getId(),rectangle);
    }


}
